package cn.tedu.vrd.controller;

import cn.tedu.vrd.mapper.ProductMapper;
import cn.tedu.vrd.vo.Product;
import cn.tedu.vrd.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
public class ProductController {
    @Autowired(required = false)
    ProductMapper mapper;

    //读取配置文件中的值赋值给dirPath变量
    @Value("${dirPath}")
    //文件夹路径
    private String dirPath;

    @RequestMapping("/product/insert")
    public int insert(Product product, MultipartFile picFile, HttpSession session) throws IOException {
        System.out.println("product = " + product + ", picFile = " + picFile + ", session = " + session);

        //得到登录的用户对象
        User u = (User) session.getAttribute("u");
        if (u==null){
            return 2; //未登录
        }
        //保存文件
        String fileName = picFile.getOriginalFilename();
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        fileName = UUID.randomUUID()+suffix;

        SimpleDateFormat f = new SimpleDateFormat("/yyyy/MM/dd/");
        //new Date()当前系统时间 /2021/12/20/
        String datePath = f.format(new Date());
        File dirFile = new File(dirPath+datePath);
        if (!dirFile.exists()){
            dirFile.mkdirs(); //如果不存在则创建
        }
        //文件完整路径
        String filePath = dirPath+datePath+fileName;
        //保存文件
        picFile.transferTo(new File(filePath));
        product.setUrl(datePath+fileName); //把包含日期路径的图片路径封装
        product.setCreated(new Date()); //发布日期
        mapper.insert(product); //把作品信息保存到数据库
        return 1;//发布成功
    }

    @RequestMapping("/product/select")
    public List<Product> select(){
        return mapper.select();
    }

    @RequestMapping("/product/delete")
    public void delete(int id){
        //通过id查到图片的url
        String url = mapper.selectUrlById(id);
        //删除文件
        new File(dirPath+url).delete();
        mapper.delete(id);
    }
    @RequestMapping("/product/select/view")
    public List<Product> selectView(){
        return mapper.selectView();
    }

    @RequestMapping("/product/select/like")
    public List<Product> selectLike(){
        return mapper.selectLike();
    }

    @RequestMapping("/product/selectById")
    public Product selectById(int id){
        //让浏览量+1
        mapper.view(id);
        return mapper.selectById(id);
    }
    @RequestMapping("/product/like")
    public int like(int id,HttpSession session){
        String info = (String)session.getAttribute("like"+id);
        if (info==null){ //代表没有点过赞
            mapper.like(id);
            session.setAttribute("like"+id,"like");
            return 1; //点赞成功!
        }
       return 2; //已经点过赞
    }

    @RequestMapping("/product/selectByCid")
    public List<Product> selectByCid(int cid){
        return mapper.selectByCid(cid);
    }

    @RequestMapping("/product/search")
    public List<Product> search(String wd){
        System.out.println("wd = " + wd);
        return  mapper.selectByWd(wd);

    }
}
